package circuit1;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Erik
 */
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.ArrayList;

public class View extends Observable implements ActionListener{

    FileDialog dialog;
    TextField tf;
    String path;
    Controller controller;
    Canvas canvas;
    ArrayList<IView> uiElements;
    int signalPos = 0;
    int displayPos = 5;
    int gatePos = 1;
    MouseListener ml;
    Button stepButton;
    boolean autostep = false;

    /**
     * Konstruktor, letrehozzuk a Frame-t, az elemeket, Listenerek hozzadasa stb
     * @param ic 
     */
    public View(Controller ic) {
        controller = ic;
        this.addObserver(controller);
        uiElements = new ArrayList<IView>();
        Frame frame = new Frame();
        frame.setTitle("Circuit");
        frame.setSize(900, 700);
        frame.addWindowListener(new myWindowListener());
        ml = new MyMouseListener(uiElements);

        dialog = new FileDialog(frame, "Choose a file", FileDialog.LOAD);

        Button loadButton = new Button("Load new circuit");
        loadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(true);
                path = dialog.getDirectory() + dialog.getFile();
                if (path == null) {
                    ////System.out.println("You cancelled the choice");
                } else {
                    ////System.out.println("You chose " + path);
                    setChanged();
                    notifyObservers("load");
                }
            }
        });
        stepButton = new Button("Autostep ON");
        stepButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if( autostep ){
                    setChanged();
                    notifyObservers("stepoff");
                    stepButton.setLabel("Autostep ON");
                    autostep = false;
                }
                else{
                    setChanged();
                    notifyObservers("stepon");
                    stepButton.setLabel("Autostep OFF");
                    autostep = true;
                }
            }
        });
        tf = new TextField("Application started", 64);
        frame.setLayout(new BorderLayout());
        Panel panel = new Panel(new FlowLayout());
        canvas = new Canvas();
        panel.add(loadButton);
        panel.add(stepButton);
        frame.add(panel, BorderLayout.NORTH);
        frame.add(canvas, BorderLayout.CENTER);
        frame.add(tf, BorderLayout.SOUTH);
        frame.show();
    }
    
    /**
     * Az aramkor fajl nevet adja vissza
     * @return fajl neve
     */
    public String getPath() {
        return path;
    }
    
    /**
     * A Timer ezt a metodust hivja meg
     * @param e 
     */
    public void actionPerformed(ActionEvent e) {
        for(Generator gen : controller.getModel().getGenerators()) {
                    gen.act();
            }
            controller.tickAll(50);
            reDraw();
    }

    /**
     * Egy aramkor betolteseert felelos, o rajzolja ki eloszor az aramkort
     */
    public void draw() {
        canvas.update(canvas.getGraphics());
        uiElements.clear();
        canvas.removeMouseListener(ml);
        signalPos = 0;
        displayPos = 5;
        gatePos = 1;
        for (IElement element : controller.getModel().getElements()) {
            String name = element.getClass().getSimpleName();
            IView uiElement;
            if (name.equals("And")) {
                uiElement = new AndUI((And) element, gatePos);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                gatePos += 6;
            }
            if (name.equals("Or")) {
                uiElement = new OrUI((Or) element, gatePos);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                gatePos += 6;
            }
            if (name.equals("Inverter")) {
                uiElement = new InverterUI((Inverter) element, gatePos);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                gatePos += 6;
            }
            if (name.equals("Switch")) {
                uiElement = new SwitchUI((Switch) element, signalPos, controller);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                signalPos += 6;
            }
            if (name.equals("Generator")) {
                uiElement = new GeneratorUI((Generator) element, signalPos, controller);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                signalPos += 6;
            }
            if (name.equals("Led")) {
                uiElement = new LedUI((Led) element, displayPos);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                displayPos += 6;
                if (displayPos == 29)
                    displayPos = -25;
            }
            if (name.equals("Oscilloscope")) {
                uiElement = new OscilloscopeUI((Oscilloscope) element, displayPos);
                uiElement.draw(canvas.getGraphics());
                uiElements.add(uiElement);
                displayPos += 6;
            }
            if (gatePos == 55) {
                gatePos = 2;
            }
            if (gatePos == 50) {
                gatePos = 3;
            }
            if (gatePos == 51) {
                gatePos = 4;
            }
            if (gatePos == 52) {
                gatePos = 5;
            }
        }

        for (Composite composite : controller.getModel().getCompositeElements()) {
            IView uiElement = new CompositeUI(composite, gatePos);
            uiElement.draw(canvas.getGraphics());
            uiElements.add(uiElement);
            gatePos += 6;
            if (gatePos == 49) {
                gatePos = 2;
            }
            if (gatePos == 50) {
                gatePos = 3;
            }
            if (gatePos == 51) {
                gatePos = 4;
            }
            if (gatePos == 52) {
                gatePos = 5;
            }
        }

        int num = uiElements.size();
        for (int i = 0; i < controller.getModel().getWiresFrom().size(); i++) {
            String from = controller.getModel().getWiresFrom().get(i);
            String to = controller.getModel().getWiresTo().get(i);
            WireUI wui;
            int posFrom = 0, posTo = 0;
            for (int j = 0; j < num; j++) {
                IView ui = uiElements.get(j);
                if (ui.getID().equals(from)) {
                    posFrom = ui.getPos();
                }
                if (ui.getID().equals(to)) {
                    posTo = ui.getPos();
                }
            }
            wui = new WireUI(posFrom, posTo);
            wui.draw(canvas.getGraphics());
            uiElements.add(wui);
        }
        canvas.addMouseListener(ml);
    }
    /**
     * Ujra rajzolja az aramkort
     */
    public void reDraw() {
        canvas.update(canvas.getGraphics());
        for (IView ui : uiElements) {
            ui.draw(canvas.getGraphics());
        }

    }
    
}
